package com.impetus.project.skilltest.server.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.impetus.project.skilltest.common.SkillTestException;
import com.impetus.project.skilltest.common.dataobject.Candidate;
import com.impetus.project.skilltest.server.util.HibernateUtil;

public class CandidateDAO {
	
	private static Logger logger = Logger.getLogger(CandidateDAO.class.getName());
	private static SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
	
	@SuppressWarnings("unchecked")
	public static Candidate getCandidateByUserNamePassword(String userName, String password )
		throws SkillTestException
	{
		Candidate candidate = null;
		try{
			List<Candidate> candidateList = new ArrayList<Candidate>();
			sessionFactory.getCurrentSession().beginTransaction();
			candidateList = sessionFactory.getCurrentSession()
			.createCriteria(Candidate.class)
			.add(Restrictions.like("candidateid", userName))
			.add(Restrictions.like("passwd", password))
			.list();
			
			candidate = candidateList.get(0);
		}catch(Exception e)
		{
			logger.log(Level.INFO, e.getMessage());
			throw new SkillTestException("Login failed: Either the username or password specified is incorrect.");
		}
				
		return candidate;		
	}
	
	@SuppressWarnings("unchecked")
	public static List<Candidate> getCandidateByCandidateId ( String candidateId )
	throws SkillTestException
	{
		List<Candidate> candidateList = new ArrayList<Candidate>();
		
		try{
			sessionFactory.getCurrentSession().beginTransaction();
			candidateList = sessionFactory.getCurrentSession()
			.createCriteria(Candidate.class)
			.add(Restrictions.like("CANDIDATEID", candidateId))
			.add(Restrictions.like("IS_ENABLED", "Y"))
			.list();
			
		}catch(Exception e)
		{
			logger.log(Level.INFO, e.getMessage());
			throw new SkillTestException(e);
		}
		
		return candidateList;
		
	}
	
	public static void saveUpdateCandidate(Candidate candidate)
	throws SkillTestException
	{
		try
		{
			sessionFactory.getCurrentSession().beginTransaction();
			sessionFactory.getCurrentSession().saveOrUpdate(candidate);
			sessionFactory.getCurrentSession().getTransaction().commit();
			
		}catch(Exception e)
		{
			sessionFactory.getCurrentSession().getTransaction().rollback();
			logger.log(Level.INFO, e.getMessage());
			throw new SkillTestException(e);
		}
	}
	
	public static void saveCandidate(Candidate candidate)
	throws SkillTestException
	{
		try
		{
			sessionFactory.getCurrentSession().beginTransaction();
			sessionFactory.getCurrentSession().save(candidate);	
			sessionFactory.getCurrentSession().getTransaction().commit();
			
		}catch(Exception e)
		{
			sessionFactory.getCurrentSession().getTransaction().rollback();
			logger.log(Level.INFO, e.getMessage());
			throw new SkillTestException(e);
		}
	}
	
	public static void deleteCandidate(Candidate candidate)
	throws SkillTestException
	{
		try
		{
			sessionFactory.getCurrentSession().beginTransaction();
			sessionFactory.getCurrentSession().delete(candidate);
			sessionFactory.getCurrentSession().getTransaction().commit();
			
		}catch(Exception e)
		{
			sessionFactory.getCurrentSession().getTransaction().rollback();
			logger.log(Level.INFO, e.getMessage());
			throw new SkillTestException(e);
		}
	}
}
